package models

import (
	"gorm.io/gorm"
	"time"
)

type MenuFoodIngredient struct {
	ID          int       `gorm:"primaryKey;autoIncrement;column:id" json:"id"`
	Name        string    `gorm:"size:255;column:name;index:idx_name,unique" json:"name"`
	Type        int       `gorm:"column:type;index:idx_name,unique" json:"type"`
	Image       string    `gorm:"size:255;column:image" json:"image"`
	Description string    `gorm:"size:255;column:description" json:"description"`
	MarkSort    string    `gorm:"size:255;column:mark_sort" json:"mark_sort"`
	Unit        string    `gorm:"size:50;column:unit" json:"unit"`
	Tag         string    `gorm:"size:255;column:tag" json:"tag"`
	Status      int       `gorm:"column:status" json:"status"`
	Remark      string    `gorm:"size:255;column:remark" json:"remark"`
	CreatedAt   time.Time `gorm:"column:created_at" json:"created_at"`
	UpdatedAt   time.Time `gorm:"column:updated_at" json:"updated_at"`
}

// TableName 设置表名
func (MenuFoodIngredient) TableName() string {
	return "menu_food_ingredient"
}

// 食材类型常量定义
const (
	IngredientTypeVegetable = 1 // 蔬菜类
	IngredientTypeMeat      = 2 // 鱼、禽、蛋、肉类
	IngredientTypeDryGoods  = 3 // 干货类
	IngredientTypeFrozen    = 4 // 冷冻类
	IngredientTypeSeasoning = 5 // 调料类(其他)
)

// 食材类型映射
var IngredientTypes = map[int]string{
	1: "蔬菜类",
	2: "鱼、禽、蛋、肉类",
	3: "干货类",
	4: "冷冻类",
	5: "调料类(其他)",
}

// 状态常量定义
const (
	IngredientStatusNormal = 1 // 正常
	IngredientStatusHidden = 2 // 不可见
)

// GetIngredientsByType 根据类型查询食材(分页)
func GetIngredientsByType(db *gorm.DB, ingredientType int, search string, pagination *Pagination) (*PageResult, error) {
	var ingredients []MenuFoodIngredient
	var total int64
	query := db.Model(&MenuFoodIngredient{})
	if search != "" {
		query = query.Where("name LIKE ?", "%"+search+"%")
	}
	query.Where("status = 1") // 默认查询正常的
	if ingredientType != 0 {
		query = query.Where("type = ?", ingredientType)
	}
	// 先查询总数
	query.Count(&total)
	query.Order("id DESC").Offset(pagination.Offset()).Limit(pagination.PageSize).Find(&ingredients)

	return NewPageResult(total, ingredients, pagination.Page, pagination.PageSize), nil
}
